package io.renren.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import io.renren.entity.CustomBaseinfoEntity;
import io.renren.entity.VO.CustomBaseinfoVO;
import io.renren.entity.VO.CustomSettlementEntityVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.entity.CustomSettlementEntity;
import io.renren.service.CustomSettlementService;
import io.renren.utils.PageUtils;
import io.renren.utils.Query;
import io.renren.utils.R;


/**
 * 客户结算点管理
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-06-19 15:02:00
 */
@RestController
@RequestMapping("customsettlement")
public class CustomSettlementController {

	/**
	 * 日志对象
	 */
	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private CustomSettlementService customSettlementService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("customsettlement:list")
	public R list(@RequestParam Map<String, Object> params){
		//与b1进行数据同步
		try {
			customSettlementService.startSynToB1();
		} catch (Exception e) {
			//logger.info("与b1同步发生异常，数据可能不是最新的，请联系系统管理员！");
			logger.error(e.getMessage(),e);
			e.printStackTrace();
		}

		//查询列表数据
        Query query = new Query(params);

		List<CustomSettlementEntity> customSettlementList = customSettlementService.queryList(query);
		int total = customSettlementService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(customSettlementList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("customsettlement:info")
	public R info(@PathVariable("id") String id){
		CustomSettlementEntity customSettlement = customSettlementService.queryObject(id);
		
		return R.ok().put("customSettlement", customSettlement);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("customsettlement:save")
	public R save(@RequestBody CustomSettlementEntity customSettlement){
		customSettlementService.save(customSettlement);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("customsettlement:update")
	public R update(@RequestBody CustomSettlementEntity customSettlement){
		customSettlementService.update(customSettlement);

		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("customsettlement:delete")
	public R delete(@RequestBody String[] ids){
		customSettlementService.deleteBatch(ids);
		
		return R.ok();
	}

	@RequestMapping("/getListinfo")
	@RequiresPermissions("customsettlement:info")
	public R getListinfo(){
		Map<String, Object> map = customSettlementService.getListinfo();
		return R.ok(map);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/queryByIndex/{index}")
	@RequiresPermissions("customsettlement:info")
	public R queryByIndex(@PathVariable("index") String index){
		String value = customSettlementService.queryByIndex(index);
		return R.ok().put("indexToValue", value);
	}

	/**
	 * 获取树菜单
	 */
	@RequestMapping("/getTree")
	@RequiresPermissions("custombaseinfo:list")
	public R getTree(){
		List<CustomSettlementEntity> list = customSettlementService.getTree();
		List<CustomSettlementEntity> res = new ArrayList<>();
		for (CustomSettlementEntity cus : list) {
			CustomSettlementEntityVO vo = new CustomSettlementEntityVO();
			vo.setName(cus.getSettlementName());
			vo.setUnitId(cus.getId());
			vo.setId(cus.getId());
			res.add(vo);
		}
		return R.ok().put("list",res);
	}

	/**
	 * 获取树菜单
	 *//*
	@RequestMapping("/getSelectList")
	@RequiresPermissions("custombaseinfo:list")
	public R getSelectList(){
		Map<String, Object> map = customSettlementService.getSelectList();
		return R.ok().put("map",map);
	}*/
	
}
